from openai import OpenAI
from pydantic import BaseModel
from typing import List


class SynonymsResponse(BaseModel):
    synonyms: List[str]


def openai_api_list(text, openAI_key, model_name='gpt-4.1', base_url=None):
    openai_api_key = openAI_key
    if base_url is not None:
        client = OpenAI(api_key=openai_api_key, base_url=base_url)
    else:
        client = OpenAI(api_key=openai_api_key)

    chat_response = client.chat.completions.parse(
        # chat_response = client.chat.completions.create(
        model=model_name,
        seed=42,
        messages=[
            {"role": "system", "content": "You are a helpful biomedical scientist."},
            {"role": "user", "content": text},
        ],
        response_format=SynonymsResponse
    )

    return chat_response.choices[0].message.parsed.synonyms


# Example usage
if __name__ == "__main__":
    openAI_key = ""
    base_url = "https://api.v3.cm/v1"

    # prompt="Please translate the following sentence into Chinese, French, Spanish, Portuguese, German, Arabic, and Japanese. Make sure you fully understand the original meaning before translating. The translated sentences must convey exactly the same meaning as the original. The original sentence is:\n"

    prompt = "Please generate 5 paraphrased versions of the following sentence. Make sure you fully understand the original meaning before rewriting. The paraphrased sentences must convey exactly the same meaning as the original. The original sentence is:\n"

    original_sentence = "3-Hydroxybutyrate from Nightingale Health data. Biomarker group: Ketone bodies"
    text = prompt + original_sentence
    print(text)
    synonyms = openai_api_list(text, openAI_key, base_url=base_url)
    print(synonyms)
